function findKpinfo() {
    // 开票信息
    var plistColumns = [{
        id: 'contract_id',
        title: '合同编号',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'invoice_buget',
        title: '开票金额',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
          }
    }, {
        id: 'user_name',
        title: '开票人',
        width: 80,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'invoice_time',
        title: '开票时间',
        width: 100,
        type: 'date',
        format:'yyyy-MM-dd',
        otype:'time_stamp_ms',
        columnClass: 'text-center',
    }, {
        id: 'invoice_comments',
        title: '发票内容',
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'invoice_sl',
        title: '税率',
        width: 80,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record) {
            return record.invoice_sl+"%";
        }
    }, {
        id: 'operation',
        title: '操作',
        type: 'string',
        width: 60,
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '无';
            //计算相差多少天
            var d = new Date();
            start = record.createTime.replace(/-/g, "/");
            var startdate = new Date(start);
            var time = d.getTime() - startdate.getTime();
            var days = parseInt(time / (1000 * 60 * 60 * 24));
            //大于三天则不能修改和删除
            if (days <= 1) {
                content = '<button class="btn btn-xs btn-danger" onclick="delInvoice(' + record.id + ');"><i class="fa fa-trash-o"></i>  删除</button>';
            }
            // content += '<button class="btn btn-xs btn-success" onclick="edpc(' + record.id + ');"><i class="icon-edit"></i>  编辑</button>';
            // content += '  ';
            return content;
        }
    }];
    $certusUtils.ajax("findInvos",{"projId":proId},function (response) {
        var plistOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '开票信息',
            datas: response.data,
            columns: plistColumns,
            gridContainer: 'plistContainer',
            toolbarContainer: 'plistToolBarContainer',
            tools: '',
            pageSize: 10,
            pageSizeLimit: [10, 15, 20]
        };
        var plistGrid = $.fn.dlshouwen.grid.init(plistOption);
        plistGrid.load();
    });

    // 回款信息
    var recbleColumns = [{
        id: 'contract_id',
        title: '合同编号',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'user_name',
        title: '经办人',
        width: 80,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'payment_buget',
        title: '回款金额',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
          }
    }, {
        id: 'payment_time',
        title: '回款日期',
        width: 100,
        columnClass: 'text-center',
        type: 'date',
        format:'yyyy-MM-dd',
        otype:'time_stamp_ms'
    }, {
        id: 'payment_comments',
        title: '回款说明',
        type: 'string',
        columnClass: 'text-center',
    },{
        id: 'create_time',
        title: '上传时间',
        width: 100,
        columnClass: 'text-center',
        type: 'date',
        format:'yyyy-MM-dd',
        otype:'time_stamp_ms'
    }, {
        id: 'operation',
        title: '操作',
        type: 'string',
        width: 60,
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '无';
            //计算相差多少天
            var d = new Date();
            start = record.createTime.replace(/-/g, "/");
            var startdate = new Date(start);
            var time = d.getTime() - startdate.getTime();
            var days = parseInt(time / (1000 * 60 * 60 * 24));
            //大于三天则不能修改和删除
            if (days <= 1) {
                content = '<button class="btn btn-xs btn-danger" onclick="delPayments(' + record.id + ');"><i class="fa fa-trash-o"></i>  删除</button>';
            }
            // content += '<button class="btn btn-xs btn-success" onclick=""><i class="icon-edit"></i>  编辑</button>';
            // content += '  ';
            return content;
        }
    }];

    $certusUtils.ajax("findPayments",{"projId":proId},function (response) {
        var recbleOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '回款信息列表',
            datas: response.data,
            columns: recbleColumns,
            gridContainer: 'recbleContainer',
            tools: '',
        };
        var recbleGrid = $.fn.dlshouwen.grid.init(recbleOption);
        recbleGrid.load();
    });

    this.delPayments = function (id) {
        layer.confirm('确认删除此数据？', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            $certusUtils.ajax("delPaymet",{"id":id},function () {
                layer.msg("删除成功");
                toLoadHtml(102);
            });
        })
    };

    //已删除开票信息
    var delkpColumns = [{
        id: 'contractId',
        title: '合同编号',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'invoiceBuget',
        title: '开票金额',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
        }
    }, {
        id: 'userName',
        title: '开票人',
        width: 80,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'invoiceTime',
        title: '开票时间',
        width: 100,
        type: 'date',
        format:'yyyy-MM-dd',
        otype:'time_stamp_ms',
        columnClass: 'text-center',
    }, {
        id: 'invoiceComments',
        title: '发票内容',
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'invoiceSl',
        title: '税率',
        width: 80,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record) {
            return record.invoiceSl+"%";
        }
    }, {
        id: 'delTime',
        title: '删除时间',
        width: 100,
        type: 'date',
        format:'yyyy-MM-dd',
        otype:'time_stamp_ms',
        columnClass: 'text-center'
    }, {
        id: 'delUserName',
        title: '删除人',
        width: 80,
        type: 'string',
        columnClass: 'text-center'
    }];
    $certusUtils.ajax("findProDelInvoices",{"proId":proId},function (response) {
        var plistOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '已删除开票信息',
            datas: response.data,
            columns: delkpColumns,
            gridContainer: 'delkpContainer',
            tools: '',
        };
        var plistGrid = $.fn.dlshouwen.grid.init(plistOption);
        plistGrid.load();
    });

    // 回款信息
    var delhksColumns = [{
        id: 'contractId',
        title: '合同编号',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'dearUserName',
        title: '经办人',
        width: 80,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'paymentBuget',
        title: '回款金额',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
        }
    }, {
        id: 'paymentTime',
        title: '回款日期',
        width: 100,
        columnClass: 'text-center',
        type: 'date',
        format:'yyyy-MM-dd',
        otype:'time_stamp_ms'
    }, {
        id: 'paymentComments',
        title: '回款说明',
        type: 'string',
        columnClass: 'text-center',
    },{
        id: 'createTime',
        title: '上传时间',
        width: 100,
        columnClass: 'text-center',
        type: 'date',
        format:'yyyy-MM-dd',
        otype:'time_stamp_ms'
    }, {
        id: 'delTime',
        title: '删除时间',
        width: 100,
        type: 'date',
        format:'yyyy-MM-dd',
        otype:'time_stamp_ms',
        columnClass: 'text-center',
    }, {
        id: 'delUserName',
        title: '删除人',
        width: 80,
        type: 'string',
        columnClass: 'text-center'
    }];

    $certusUtils.ajax("findProDelPayments",{"proId":proId},function (response) {
        var recbleOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '已删除回款信息列表',
            datas: response.data,
            columns: delhksColumns,
            gridContainer: 'delhkContainer',
            tools: '',
        };
        var recbleGrid = $.fn.dlshouwen.grid.init(recbleOption);
        recbleGrid.load();
    });

    // 合同信息-统计数据
    var hlistColumns = [{
        id: 'contractNo',
        title: '合同编号',
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'contractAmout',
        title: '合同金额',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (value==""){
                value=0;
            }
            var content = '￥' + value + '元';
            return content;
          }
    }, {
        id: 'invoiceBuget',
        title: '开票总金额',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (value==""){
                value=0;
            }
            var content = '￥' + value + '元';
            return content;
          }
    }, {
        id: 'syKpBuget',
        title: '剩余开票金额',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (value==""){
                value=0;
            }
            var content = '￥' + value + '元';
            return content;
          }
    }, {
        id: 'paymentBuget',
        title: '已收款',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (value==""){
                value=0;
            }
            var content = '￥' + value + '元';
            return content;
          }
    }, {
        id: 'sypmBuget',
        title: '未收款',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (value==""){
                value=0;
            }
            var content = '￥' + value + '元';
            return content;
          }
    }];
    // {
    //     id: 'operation',
    //         title: '操作',
    //     type: 'string',
    //     width: 60,
    //     columnClass: 'text-center',
    //     resolution: function (value, record, column, grid, dataNo, columnNo) {
    //     var content = '';
    //     content += '<button class="btn btn-xs btn-primary" onclick=""><i class="icon-eye-open"></i>  查看</button>';
    //     // content += '  ';
    //     // content += '<button class="btn btn-xs btn-danger" onclick="down(' + record.id + ');"><i class="fa fa-trash-o"></i>  删除</button>';
    //     return content;
    // }
    // }
    $certusUtils.ajax("findBugetTjInfo",{"projId":proId},function (response) {
        var hlistOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '合同信息-统计数据',
            datas: response.data,
            columns: hlistColumns,
            gridContainer: 'hlistContainer',
            tools: ''
        };
        var hlistGrid = $.fn.dlshouwen.grid.init(hlistOption);
        hlistGrid.load();
    });
}
$('#invoiceTime').datepicker({
    format: 'yyyy-mm-dd'
});
$('#paymentTime').datepicker({
    format: 'yyyy-mm-dd'
});

function addKpinfo() {
    $certusUtils.ajax("findSpUserList",{},function (response) {
        var nhlTeams = [];
        var data = response.data;
        for (var i=0;i<data.length;i++){
            var arr={};
            arr.name=data[i].userName+"["+data[i].positonName+"]";
            arr.id = data[i].userId;
            nhlTeams.push(arr);
        }
        //自动补全
        var nhl = $.map(nhlTeams, function (obj) {
            return {
                value: obj.name,
                data: { category: obj.id }
            };
        });
        // Initialize autocomplete with local lookup:
        $('#autocomplete').devbridgeAutocomplete({
            lookup: nhl,
            minChars: 1,
            onSelect: function (suggestion) {
                $("#invoiceUserId").val(suggestion.data.category);
            },
            showNoSuggestionNotice: true,
            noSuggestionNotice: 'Sorry, no matching results'
        });
    });

    var contractArr = [];
    $certusUtils.ajax("contractProgresss",{"projId":proId},function (response) {
        var data = response.data;
        var html="";
        for (var i=0;i<data.length;i++){
            var arr={};
            arr.contractNo = data[i].contractInfo.contractNo;
            arr.contractAmount = data[i].contractInfo.contractAmount;
            arr.kpTotalMoney = data[i].kpTotalMoney;
            var ctinfo = data[i].contractInfo;
            $("#contractId").append("<option value='"+ctinfo.contractNo+"'>"+ctinfo.contractNo+"</option>");
            contractArr.push(arr);
        }
    });

    var htMoney = 0;
    this.getAmoutMoney = function (contractNo) {
        if (contractNo==""){
            $("#invoiceBuget").attr("placeholder","具体金额");
        }else{
            for (var i = 0;i<contractArr.length;i++){
                if (contractNo == contractArr[i].contractNo){
                    htMoney = contractArr[i].contractAmount-contractArr[i].kpTotalMoney;
                    if (htMoney<0){
                        htMoney=0;
                    }
                    $("#invoiceBuget").attr("placeholder","未开票金额:"+Number(htMoney).toFixed(2));
                }
            }
        }
    };
    
    $(".submit").on('click',function () {
        if ($("#contractId").val()==""){
            layer.msg("请选择合同编号");
            return;
        }
        if ($("#invoiceBuget").val()==""){
            layer.msg("开票金额不能为空");
            return;
        }
        if (Number($("#invoiceBuget").val())>htMoney){
            layer.msg("开票金额不能超过未开票金额");
            return;
        }

        if ($("#invoiceUserId").val()==""){
            layer.msg("开票人不能为空");
            return;
        }
        if ($("#invoiceTime").val()==""){
            layer.msg("开票时间不能为空");
            return;
        }
        if ($("#invoiceComments").val()==""){
            layer.msg("开票内容不能为空");
            return;
        }
        if ($("#invoiceComments").length>500){
            layer.msg("开票内容不能大于500字");
            return;
        }
        if ($("#invoiceSl").val()==""){
            layer.msg("税率不能为空");
            return;
        }
        var arr =$("form").serializeArray();
        var data = {};
        for (var i=0;i<arr.length;i++){
            data[arr[i].name] = arr[i].value;
        };
        data.proId = proId;
        $certusUtils.ajax("addInvoice",{"projId":proId,"param":JSON.stringify(data)},function (response) {
            layer.msg("添加成功",{"time":1000});
            toLoadHtml(102);
        })
    })
}

function delInvoice(id) {
    layer.confirm('确认删除此数据？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $certusUtils.ajax("delInvoice",{"projId":proId,"id":id},function (response) {
            layer.msg("删除成功");
            toLoadHtml(102);
        });
    })
}

function toBack() {
    toLoadHtml(102);
}

function addHkinfo() {
    $certusUtils.ajax("findSpUserList",{},function (response) {
        var nhlTeams = [];
        var data = response.data;
        for (var i=0;i<data.length;i++){
            var arr={};
            arr.name=data[i].userName+"["+data[i].positonName+"]";
            arr.id = data[i].userId;
            nhlTeams.push(arr);
        }
        //自动补全
        var nhl = $.map(nhlTeams, function (obj) {
            return {
                value: obj.name,
                data: { category: obj.id }
            };
        });
        // Initialize autocomplete with local lookup:
        $('#autocomplete').devbridgeAutocomplete({
            lookup: nhl,
            minChars: 1,
            onSelect: function (suggestion) {
                $("#dearUserId").val(suggestion.data.category);
            },
            showNoSuggestionNotice: true,
            noSuggestionNotice: 'Sorry, no matching results'
        });
    });

    var contractArr = [];
    $certusUtils.ajax("contractProgresss",{"projId":proId},function (response) {
        var data = response.data;
        var html="";
        for (var i=0;i<data.length;i++){
            var ctinfo = data[i].contractInfo;
            var arr={};
            arr.contractNo = data[i].contractInfo.contractNo;
            arr.contractAmount = data[i].contractInfo.contractAmount;
            arr.hkTotalMoney = data[i].hkTotalMoney;
            $("#contractId").append("<option value='"+ctinfo.contractNo+"'>"+ctinfo.contractNo+"</option>");
            contractArr.push(arr);
        }
    });

    var hkMoney = 0;
    this.getHkMoney = function (contractNo) {
        for (var i = 0;i<contractArr.length;i++){
            if (contractNo == contractArr[i].contractNo){
                hkMoney = contractArr[i].contractAmount-contractArr[i].hkTotalMoney;
                if (hkMoney<0){
                    hkMoney=0;
                }else{
                    hkMoney = hkMoney.toFixed(2);
                }
                $("#paymentBuget").attr("placeholder","未回款金额:"+hkMoney);
            }else {
                $("#paymentBuget").attr("placeholder","");
            }
        }
    };

    $(".psubmit").on('click',function () {
        if ($("#contractId").val()==""){
            layer.msg("请选择合同编号");
            return;
        }
        if ($("#paymentType").val()==""){
            layer.msg("请选择回款类型");
            return;
        }

        if ($("#dearUserId").val()==""){
            layer.msg("经办人不能为空");
            return;
        }
        if ($("#paymentBuget").val()==""){
            layer.msg("回款金额不能为空");
            return;
        }

        if (Number($("#paymentBuget").val())<=0){
            layer.msg("回款金额不能小于等于0元");
            return;
        }
        if (Number($("#paymentBuget").val())>hkMoney){
            layer.msg("回款金额不能超过未回款金额");
            return;
        }

        if ($("#paymentTime").val()==""){
            layer.msg("回款日期不能为空");
            return;
        }
        if ($("#paymentComments").length>500){
            layer.msg("回款说明不能大于500字");
            return;
        }
        if ($("#paymentComments").val()==""){
            layer.msg("回款说明不能为空");
            return;
        }

        var arr =$("form").serializeArray();
        var data = {};
        for (var i=0;i<arr.length;i++){
            data[arr[i].name] = arr[i].value;
        };
        data.proId = proId;
        $certusUtils.ajax("addPayment",{"projId":proId,"param":JSON.stringify(data)},function (response) {
            layer.msg("添加成功",{"time":1000});
            toLoadHtml(102);
        })
    })
}